package com.focusai.efairy.ui.activity.project;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.dev.DevType;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;

import java.util.List;

/**
 * Created by luo_guangwen on 2017/9/18.
 */

public class ProjectSelectDevTypeActivity extends SwipeBackBaseActivity {
    public static final String KEY_DEV_TYPE_ITEM = "key_dev_type_item";
    private RecyclerView mRecyclerView;
    private SelectDevTypeAdapter mAdapter;
    private List<DevType> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_recyc_list);
        initView();
        setDefaultValues();
    }

    @Override
    protected void initView() {
        mRecyclerView = findView(R.id.recycler_view);

    }

    @Override
    public void initTitte() {
        setCenterTitleName("选择设备类型");
    }

    @Override
    protected void setDefaultValues() {
        mAdapter = new SelectDevTypeAdapter();
        mList = DevType.getDevTypes();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.refreshData(mList);
        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseRecyclerViewHolder holder) {
                int position = mRecyclerView.getChildLayoutPosition(holder.itemView);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable(KEY_DEV_TYPE_ITEM, mList.get(position));
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    static class SelectDevTypeAdapter extends BaseRecyclerAdapter<DevType> {

        @Override
        protected int getItemLayoutId() {
            return R.layout.item_select_dev_type;
        }

        @Override
        protected void onBindBaseRecyclerViewHolder(BaseRecyclerViewHolder holder, int position) {
            holder.setText(R.id.text_dev_type, mList.get(position).name);
        }
    }
}
